// 获取应用实例
const app = getApp()
// 引入http.js
const http = require('../../utils/http')

Page({
  data: {
    title: "",
    desctription: "",
    detail: "",
    pk_notebook: "",

    showDialog: false,
    buttons: [{
      type: 'default',
      text: '取消',
      value: 'cancle'
    }, {
      type: 'warn',
      text: '删除',
      value: 'delete'
    }]
  },
  onLoad(options) {
    console.log(options.pk_notebook);
    if(options.pk_notebook) {
      // 有主键，表示修改或查看详情
      this.setData({
        pk_notebook: options.pk_notebook
      })
      // 发起请求获取笔记详情
      http.get({
        url: "/notebook/getNoteByPk",
        data: {pk_notebook: this.pk_notebook},
        successCallback(res) {
          console.log(res);
        },
        errorCallback(err) {
          console.log(err);
        }
      })

    } else {
      // 无主键，表示新增
    }
  },
  submitForm() {
      let title = this.data.title;
      let desctription = this.data.desctription;
      console.log(title);
      console.log(desctription);
  },
  titleInput(e) {
    //   console.log(e.detail.value)
  },
  descInput(e) {
    // console.log(e.detail.value)
  },
  detailInput(e) {
    // console.log(e.detail.value)
  },
  deleteNote() {
    console.log("删除笔记？");
    this.setData({
      showDialog: true
    })
  },
  buttontap(e) {
    console.log(e.detail);
    let item = e.detail.item;
    let value = item.value;
    if(value == 'cancle') {
      this.setData({
        showDialog: false
      })
    } else {
      // 进行删除操作
      http.post({
        url: '/notebook/deleteByPk',
        data: {
          pk_notebook: this.pk_notebook
        },
        successCallback(res) {
          console.log(res);
          console.log('返回上一页');
          wx.navigateBack();
        },
        errorCallback(err) {
          console.log(err);
          this.setData({
            showDialog: false
          })
        }
      })
    }
  }
})